package com.yserp.project.erp.store.outorder.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.record.checkorderlog.domain.CheckOrderLog;
import com.yserp.project.erp.store.outorder.domain.OutOrder;
import com.yserp.project.erp.store.outorder.domain.OutOrderDto;
import com.yserp.project.erp.store.outorder.domain.OutOrderVo;
import com.yserp.project.erp.store.outorder.service.IOutOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品出库单主Controller
 *
 * @author yserp
 * @date 2022-07-13
 */
@Controller
@RequestMapping("/store/outorder")
public class OutOrderController extends BaseController {
    private String prefix = "erp/store/outorder";

    @Autowired
    private IOutOrderService outOrderService;

    @RequiresPermissions("store:outorder:view")
    @GetMapping()
    public String outorder() {
        return prefix + "/outorder";
    }

    /**
     * 查询商品出库单主列表
     */
    @RequiresPermissions("store:outorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OutOrder outOrder) {
        startPage();
        List<OutOrder> list = outOrderService.selectOutOrderList(outOrder);
        return getDataTable(list);
    }

    /**
     * 导出商品出库单主列表
     */
    @RequiresPermissions("store:outorder:export")
    @Log(title = "商品出库单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OutOrder outOrder) {
        List<OutOrder> list = outOrderService.selectOutOrderList(outOrder);
        ExcelUtil<OutOrder> util = new ExcelUtil<OutOrder>(OutOrder.class);
        return util.exportExcel(list, "商品出库单主数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.OUT_ORDER.getPref()));
    }

    /**
     * 新增商品出库单主
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("outOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.OUT_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 销售一键出库
     * @param id 销售单主键ID
     * @param mmap 装数据的容器
     * @return
     */
    @GetMapping("/saleOutOrder")
    public String purchInstore(@RequestParam Long id, ModelMap mmap) {
        mmap.put("outOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.OUT_ORDER.getPref()));
        //获取销售单信息
        mmap.put("outOrderVo",outOrderService.saleTransfor(id));
        return prefix + "/oneClickOutOrder";
    }

    /**
     * 领料一键出库
     * @param id 生产单主键ID
     * @param mmap 装数据的容器
     * @return
     */
    @GetMapping("/productOutOrder")
    public String productInstore(@RequestParam Long id, ModelMap mmap) {
        mmap.put("outOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.OUT_ORDER.getPref()));
        //获取采购单信息
        mmap.put("outOrderVo",outOrderService.productTransfor(id));
        return prefix + "/oneClickOutOrder";
    }

    /**
     * 新增保存商品出库单主
     */
    @RequiresPermissions("store:outorder:add")
    @Log(title = "商品出库单主", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OutOrderDto outOrderDto) {
        return toAjax(outOrderService.insertOutOrder(outOrderDto));
    }

    /**
     * 修改商品出库单主
     */
    @RequiresPermissions("store:outorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        OutOrderVo outOrderVo = outOrderService.selectOutOrderById(id);
        mmap.put("outOrderVo", outOrderVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品出库单主
     */
    @RequiresPermissions("store:outorder:edit")
    @Log(title = "商品出库单主", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OutOrderDto outOrderDto) {
        return toAjax(outOrderService.updateOutOrder(outOrderDto));
    }

    /**
     * 删除商品出库单主
     */
    @RequiresPermissions("store:outorder:remove")
    @Log(title = "商品出库单主", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(outOrderService.deleteOutOrderByIds(ids));
    }


    /**
     * 跳转到审核页面
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/checkDetail")
    public String checkDetail(Long id,ModelMap mmap) {
        OutOrderVo outOrderVo = outOrderService.selectOutOrderById(id);
        mmap.put("outOrderVo", outOrderVo);
        return prefix + "/checkDetail";
    }

    /**
     * 出库单审核
     */
    @Log(title = "出库单审核", businessType = BusinessType.OTHER)
    @PostMapping("/checkStatus")
    @ResponseBody
    public AjaxResult checkStatus(@Valid CheckOrderLog checkOrderLog) {
        return toAjax(outOrderService.checkStatus(checkOrderLog));
    }


}
